﻿Imports CrystalDataHub.BLL.DataHub.BusinessObject
Imports System.Xml.Serialization
Imports System.IO
Imports System.Xml
Imports Crystal.CLL

Namespace CrystalDataHub.BLL

    Public Module mdlDataHubBLLCommon
#Region "Fields"
        Public mstrDataFolder As String
        Public mstrEDSSnapshotPath As String
        Public mstrDESnapshotPath As String
        Public mstrEDSSnapshotPathArchieve As String
        Public mstrDESnapshotPathArchieve As String
        Public mstrSnapshotDemiliter As String
        Public mstrManagedFundSuffix As String
        Public mstrConnectionstring As String
        Public mintInvestorMinSearchPercentage As Integer
        Public mintFundMinSearchPercentage As Integer
        Public mintInvestorDefaultSearchPercentage As Integer
        Public mintFundDefaultSearchPercentage As Integer
        Public mintMinSearchLength As Integer
        Public mstrANDOperator As String = "^"
        Public mstrOROperator As String = "|"
        Public marrEditableSourceTypes() As String = {SourceTypeCode.UPD.ToString, SourceTypeCode.DDI.ToString, SourceTypeCode.RSH.ToString, SourceTypeCode.PRB.ToString, _
                                                           SourceTypeCode.BKL.ToString, SourceTypeCode.RGT.ToString}
        Public marrDateSuppertedSourceTypes() As String = {DataSourceCode.DDISCRE.ToString, DataSourceCode.DDQUEST.ToString, DataSourceCode.DDCLEML.ToString, DataSourceCode.RESERCH.ToString}
        Public marrToplineNeverSOF() As String = {SourceTypeCode.REG.ToString, SourceTypeCode.EBL.ToString, SourceTypeCode.PBC.ToString, SourceTypeCode.EPB.ToString}

        Public mstrCurrentListingColPrefix As String = "Current "
        Public mstrSuggestedFinalListingColPrefix As String = "Suggested Final "
        Public mstrUpdatesColPrefix As String = "Updates "
        Public mstrDDInfoColPrefix As String = "DD Info "
        Public mstrRegisterColPrefix As String = "Registers "
        Public mstrBanklistColPrefix As String = "Banklists "
        Public mstrDDAllocationColPrefix As String = "DD Allocations "
        Public mstrResearchColPrefix As String = "Research "

        
        Public mstrSOFColPrefix As String = "~"
        Public mstrSourceDateColSuffix As String = " Date"
        Public mstrFilingDateColSuffix As String = " Filing Date"
        Public mstrClientSpecificSourceColSuffix As String = " CSSource"
        Public mstrGenericSourceColSuffix As String = " GSource"
        Public mstrSourceColSuffix As String = " Source"
        Public mstrFormulaColSuffix As String = " Formula"

        Public mstrSameDateUpdateCount As String = " SameDateUpdateCount"
        Public mstrUpdateCount As String = " UpdateCount"
        Public mstrSameDateDDInfoCount As String = " SameDateDDInfoCount"
        Public mstrDDInfoCount As String = " DDInfoCount"
        Public mstrSamePositionRegisterCount As String = " SamePositionRegisterCount"
        Public mstrSamePositionBanklistCount As String = " SamePositionBanklistCount"

        'Columns to be excluded from the filters
        Public marrExcludedFromFilters() As String = {"FundEntityID", "InvEntityID", "HasChangeLog", "LinkCount", "MInvestorTypeID", "UniFullName", "UniInvestorID", "UniFundID", _
                                                   "InvStatusCode", "FundStatusCode", "RowNumber", _
                                                   "OriginalInvestorID", "OriginalFundID", "InvestorID", "FundID", "EntityID", "ETemplateID", "ClientID", "Doubts", _
                                                   "Full Name", "UserID", "FilingDateCopy"}
        Public mstrMappedInvName As String = "Mapped Inv. Name"
        Public mstrMappedFundName As String = "Mapped Fund Name"
        Public mstrFieldSeparator As String = "|"
        Public mstrCurrentSuffix As String = "Current"
        Public mstrSuggestedFinalSuffix As String = "Suggested Final"
        Public mstrSpaceIntend As String = "    "
        Public mstrTwoSpaces As String = "  "
        Public mstrCurrrentShortcut As String = "A"
        Public mstrSuggestedShortcut As String = "S"
#End Region

#Region "Procedures"
        Public Sub SetColMappingPublicMatch(ByRef pdtColMapping As DataTable)
            If pdtColMapping.Columns.Contains("PublicMatch") = False Then
                pdtColMapping.Columns.Add("PublicMatch", GetType(String))
                For Each lRow As DataRow In pdtColMapping.Rows
                    If lRow.Item("DBFieldName") = "InvEntityID" Then
                        lRow.Item("PublicMatch") = "MatchCol1"
                    ElseIf lRow.Item("DBFieldName") = "FundEntityID" Then
                        lRow.Item("PublicMatch") = "MatchCol2"
                    ElseIf lRow.Item("DBFieldName") = "MappedInvestorName" Then
                        lRow.Item("PublicMatch") = "MatchCol3"
                    ElseIf lRow.Item("DBFieldName") = "MappedFundName" Then
                        lRow.Item("PublicMatch") = "MatchCol4"
                    End If
                Next
            End If
        End Sub

        Public Sub ManageScreenStatus(ByVal pintEntityID As Integer, ByVal pintProjectID As Integer, ByVal pintUserID As Int16, ByVal pblnEDS As Boolean, _
                                      ByVal pblnDataEntry As Boolean, ByVal pblnFL As Boolean)
            DataFactory.CreateDataBase().ExecuteNonQuery("CR_sp_PRJ_ManageScreenStatus", New Object() {pintEntityID, pintProjectID, pintUserID, pblnEDS, pblnDataEntry, pblnFL})
        End Sub

        ''' <summary>
        ''' Serializes the object to XML based on encoding and name spaces.
        ''' </summary>
        ''' <param name="serializer">XmlSerializer object 
        ''' (passing as param to avoid creating one every time)</param>
        ''' <param name="encoding">The encoding of the serialized Xml</param>
        ''' <param name="objectToSerialize">The object we want to serialize to Xml</param>
        ''' <returns></returns>
        Public Function SerializeFromObject(ByVal serializer As XmlSerializer, ByVal encoding As Text.Encoding, ByVal objectToSerialize As Object) As String
            Dim ms As New MemoryStream()
            Dim settings As New XmlWriterSettings()
            Dim writer As XmlWriter
            Dim ns As XmlSerializerNamespaces = New XmlSerializerNamespaces()
            ns.Add("", "")
            Try
                settings.Indent = False
                settings.OmitXmlDeclaration = True
                settings.Encoding = encoding
                writer = XmlWriter.Create(ms, settings)
                serializer.Serialize(writer, objectToSerialize, ns)
                Return encoding.GetString(ms.ToArray())
            Finally
                ms = Nothing
                ns = Nothing
                settings = Nothing
                writer = Nothing
            End Try
        End Function

        ''' <summary>
        ''' To set the primary key in data tables
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub SetPrimaryKey(ByRef pdtTable As DataTable)
            Dim keyColumn(1) As DataColumn
            keyColumn(0) = pdtTable.Columns("InvestorID")
            keyColumn(1) = pdtTable.Columns("FundID")
            pdtTable.PrimaryKey = keyColumn
        End Sub

#End Region

    End Module

    'Public Class GenericClass(Of T)
    '    Public Function GetXMlFromDataTable(ByVal pstData As List(Of T), ByVal larrExcludingCols As String()) As String
    '        Dim lstrData As New System.Text.StringBuilder()
    '        Dim Value As String = ""
    '        Dim RowCount As Integer = 1
    '        Dim GridRowsCount As Integer = pstData.Count
    '        Try
    '            lstrData.Append("<ROOT>")
    '            For Each lobjData As DataEntities In DirectCast(pstData, List(Of DataEntities))
    '                lstrData.Append("<Row ")
    '                For Each lcolInvestor As DataColumn In pdtData.Columns
    '                    If larrExcludingCols.Contains(lcolInvestor.ColumnName) = False Then
    '                        lstrData.Append(lcolInvestor.ColumnName.Replace(" ", ""))
    '                        lstrData.Append("=""")
    '                        If (String.IsNullOrEmpty(lrowInvestor(lcolInvestor.ColumnName).ToString())) Then
    '                            Value = " "
    '                        Else
    '                            Value = lrowInvestor(lcolInvestor.ColumnName).ToString().Trim()
    '                        End If
    '                        lstrData.Append(ReplaceSpecialChar(Value))
    '                        lstrData.Append(""" ")
    '                    End If
    '                Next
    '                lstrData.Append("/>")
    '                lstrData.Append(Environment.NewLine)
    '                RowCount = RowCount + 1
    '            Next
    '            lstrData.Append("</ROOT>")
    '            Return lstrData.ToString()
    '        Catch ex As Exception
    '            Throw ex
    '        Finally
    '            lstrData = Nothing
    '            pdtData = Nothing
    '        End Try
    '    End Function
    'End Class
End Namespace